package util;

import java.util.HashSet;
import java.util.Iterator;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Abstract Class to let a Model fire ChangeEvent
 * 
 * @author MK
 * 
 */
public abstract class ChangingModel {

	private HashSet<ChangeListener> listenerList;

	public ChangingModel() {
		listenerList = new HashSet<ChangeListener>();
	}

	public void addChangeListener(ChangeListener listener) {
		listenerList.add(listener);
	}

	public void removeChangeListener(ChangeListener listener) {
		listenerList.remove(listener);
	}

	protected void fireModelChange(Object obj) {

		HashSet<ChangeListener> listCopy;
		synchronized (this) {
			listCopy = (HashSet<ChangeListener>) listenerList.clone();
		}

		for (Iterator<ChangeListener> i = listCopy.iterator(); i.hasNext();) {
			i.next().stateChanged(new ChangeEvent(obj));
		}
	}
}